package top.yaofengqiao.springcloudsimple.demo.biz.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.yaofengqiao.dynamicthreadpool.core.thread.MonitoringThreadPoolExecutor;
import top.yaofengqiao.dynamicthreadpool.core.thread.RejectedTaskCounter;

import java.util.concurrent.*;

/**
 * @author yfq
 * @date 2024/6/21 11:25
 * @description
 */
@Configuration
public class ThreadPoolConfig {

    /**
     * 批量任务线程池
     */
    @Bean
    public ThreadPoolExecutor taskThreadPool() {
        int cpu = Runtime.getRuntime().availableProcessors();

        return new MonitoringThreadPoolExecutor(cpu,
                cpu * 2,
                30,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1000),
                Executors.defaultThreadFactory(),
                new RejectedTaskCounter());
    }

    /**
     * 用户请求处理线程池
     */
    @Bean
    public ThreadPoolExecutor clientThreadPool() {
        int cpu = Runtime.getRuntime().availableProcessors();

        return new MonitoringThreadPoolExecutor(cpu * 3,
                cpu * 23,
                30,
                TimeUnit.SECONDS,
                new SynchronousQueue<>(),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());
    }

}
